import sys
import copy
import random
from PyQt6 import QtGui
from PyQt6.QtWidgets import QWidget, QVBoxLayout, QHBoxLayout, QLabel, QMainWindow, QApplication, QMessageBox
from PyQt6.QtCore import QTimer, Qt
from random import choice

class Tetris(object):
    SHAPES={
            'Z':[[0,3],[0,4],[1,4],[1,5]],
            'S':[[1,3],[1,4],[0,4],[0,5]],
            'I':[[0,3],[0,4],[0,5],[0,6]],
            'T':[[0,3],[0,4],[1,4],[0,5]],
            'O':[[0,4],[0,5],[1,4],[1,5]],
            'L':[[0,3],[1,3],[1,4],[1,5]],
            'J':[[1,3],[1,4],[1,5],[0,5]]
            }
    
    def __init__(self):
        self.activeshape()

    def activeshape(self):
        self.nextshape=random.randint(1,7)
        if self.nextshape==1:
            self.shapestructure=[[0,3],[0,4],[1,4],[1,5]]
        elif self.nextshape==2:
            self.shapestructure=[[1,3],[1,4],[0,4],[0,5]]
        elif self.nextshape==3:
            self.shapestructure=[[0,3],[0,4],[0,5],[0,6]]
        elif self.nextshape==4:
            self.shapestructure=[[0,3],[0,4],[1,4],[0,5]]
        elif self.nextshape==5:
            self.shapestructure=[[0,4],[0,5],[1,4],[1,5]]
        elif self.nextshape==6:
            self.shapestructure=[[0,3],[1,3],[1,4],[1,5]]
        elif self.nextshape==7:
            self.shapestructure=[[1,3],[1,4],[1,5],[0,5]]
        self.irow=self.shapestructure[0][0]
        self.icolum=self.shapestructure[0][1]
        self.jrow=self.shapestructure[1][0]
        self.jcolum=self.shapestructure[1][1]
        self.krow=self.shapestructure[2][0]
        self.kcolum=self.shapestructure[2][1] 
        self.prow=self.shapestructure[3][0]
        self.pcolum=self.shapestructure[3][1]
        
    def generateshape(self):
        self.lastshape = self.nextshape
        self.nextshape=random.randint(1,7)
        if self.nextshape==1:
            self.shapestructure=[[0,3],[0,4],[1,4],[1,5]]  
        elif self.nextshape==2:
            self.shapestructure=[[1,3],[1,4],[0,4],[0,5]]
        elif self.nextshape==3:
            self.shapestructure=[[0,3],[0,4],[0,5],[0,6]]
        elif self.nextshape==4:
            self.shapestructure=[[0,3],[0,4],[1,4],[0,5]]
        elif self.nextshape==5:
            self.shapestructure=[[0,4],[0,5],[1,4],[1,5]] 
        elif self.nextshape==6:
            self.shapestructure=[[0,3],[1,3],[1,4],[1,5]]
        elif self.nextshape==7:
            self.shapestructure=[[1,3],[1,4],[1,5],[0,5]]
        self.irow=self.shapestructure[0][0]
        self.icolum=self.shapestructure[0][1]
        self.jrow=self.shapestructure[1][0]
        self.jcolum=self.shapestructure[1][1]
        self.krow=self.shapestructure[2][0]
        self.kcolum=self.shapestructure[2][1] 
        self.prow=self.shapestructure[3][0]
        self.pcolum=self.shapestructure[3][1]
        
class Window(QMainWindow):
    def __init__(self):
        super(Window, self).__init__()
        self.task = Tetris()
        self.rows = 20
        self.columns = 10
        self.point_piece = 10
        self.points_line = 100
        self.label = {}
        self.score = 0
        self.lines = 0
        self.board = []
        for i in range(25):
            self.board.append([])
            for j in range(15):
                self.board[i].append(0)
        self.task.shapestructure = [[0,0],[0,0],[0,0],[0,0]]
        self.game_running = False
        self.message = "New game is loaded"
        self.setGeometry(800,180,400,700)
        self.setWindowTitle("Tetris")
        self.setWindowIcon(QtGui.QIcon('favicon.png'))
        self.main_page()
        self.timer = QTimer()
        self.timer.timeout.connect(self.timer_event)
        self.timer.start(800)

    def main_page(self):
        central_widget = QWidget()
        self.setCentralWidget(central_widget) 
        horizontal_layout = QHBoxLayout(central_widget)
        horizontal_layout.setSpacing(50)
        vertical_board = QVBoxLayout()
        horizontal_layout.addLayout(vertical_board)
        for row in range(0, self.rows):
            horizontal_board = QHBoxLayout()
            horizontal_board.setSpacing(0)
            for column in range(0, self.columns):
                self.label[row, column] = QLabel(self)
                self.label[row, column].setFixedHeight(40)
                self.label[row, column].setFixedWidth(40)
                self.label[row, column].setStyleSheet("background-color:lightGrey;border: 1px inset darkGrey")
                horizontal_board.addWidget(self.label[row, column])
            vertical_board.addLayout(horizontal_board)
            vertical_board.setSpacing(0)
        self.show()
        self.setFixedSize(self.size())

    def timer_event(self):
        print('timer_event',self.task.irow,self.task.jrow,self.task.krow,self.task.prow)
        self.irow0 = self.task.irow
        self.icolum0 = self.task.icolum
        self.jrow0 = self.task.jrow
        self.jcolum0 = self.task.jcolum
        self.krow0 = self.task.krow
        self.kcolum0 = self.task.kcolum
        self.prow0 = self.task.prow
        self.pcolum0 = self.task.pcolum
        self.task.irow+=1
        self.task.jrow+=1
        self.task.krow+=1
        self.task.prow+=1
        if self.task.irow > 19 or self.task.jrow > 19 or self.task.krow > 19 or  self.task.prow > 19:
            self.task.irow = self.irow0
            self.task.icolum = self.icolum0
            self.task.jrow = self.jrow0
            self.task.jcolum = self.jcolum0
            self.task.krow = self.krow0
            self.task.kcolum = self.kcolum0
            self.task.prow = self.prow0
            self.task.pcolum = self.pcolum0
        self.update_block()
        self.judgment()

    def judgment(self):
        self.shapestructure=[[0,0],[0,0],[0,0],[0,0]]
        self.shapestructure[0][0]=self.task.irow
        self.shapestructure[0][1]=self.task.icolum
        self.shapestructure[1][0]=self.task.jrow
        self.shapestructure[1][1]=self.task.jcolum
        self.shapestructure[2][0]=self.task.krow
        self.shapestructure[2][1]=self.task.kcolum
        self.shapestructure[3][0]=self.task.prow
        self.shapestructure[3][1]=self.task.pcolum
        ground = False
        for i in range(4):
            x = self.shapestructure[i][0]
            y = self.shapestructure[i][1]
            if self.board[x+1][y] != 0:
                ground = True
                break
            if x == 19 :
                ground = True
                break
        if ground:
            for i in range(4):
                x = self.shapestructure[i][0]
                y = self.shapestructure[i][1]
                self.board[x][y]=self.task.nextshape
                self.task.generateshape()
            
    def update_block(self):

        self.label[self.irow0,self.icolum0].setStyleSheet("background-color:lightGrey;border: 1px inset darkGrey")
        self.label[self.jrow0,self.jcolum0].setStyleSheet("background-color:lightGrey;border: 1px inset darkGrey")
        self.label[self.krow0,self.kcolum0].setStyleSheet("background-color:lightGrey;border: 1px inset darkGrey")
        self.label[self.prow0,self.pcolum0].setStyleSheet("background-color:lightGrey;border: 1px inset darkGrey")
        self.label[self.task.irow,self.task.icolum].setStyleSheet("background-color:red;border: 1px inset darkGrey")
        self.label[self.task.jrow,self.task.jcolum].setStyleSheet("background-color:red;border: 1px inset darkGrey")
        self.label[self.task.krow,self.task.kcolum].setStyleSheet("background-color:red;border: 1px inset darkGrey")
        self.label[self.task.prow,self.task.pcolum].setStyleSheet("background-color:red;border: 1px inset darkGrey")

    def keyPressEvent(self, event):
        pressedkey = event.key()     
        if pressedkey == Qt.Key.Key_Up:
            print('Key_up')
            #self.rotate()
            event.accept()
        elif pressedkey == Qt.Key.Key_Down:
            print('Key_Down')
            self.irow0 = self.task.irow
            self.icolum0 = self.task.icolum
            self.jrow0 = self.task.jrow
            self.jcolum0 = self.task.jcolum
            self.krow0 = self.task.krow
            self.kcolum0 = self.task.kcolum
            self.prow0 = self.task.prow
            self.pcolum0 = self.task.pcolum
            self.task.irow+=1
            self.task.jrow+=1
            self.task.krow+=1
            self.task.prow+=1
            self.update_block()
            self.judgment()
            event.accept()
        elif pressedkey == Qt.Key.Key_Left:
            print('Key_Left')
            self.irow0 = self.task.irow
            self.icolum0 = self.task.icolum
            self.jrow0 = self.task.jrow
            self.jcolum0 = self.task.jcolum
            self.krow0 = self.task.krow
            self.kcolum0 = self.task.kcolum
            self.prow0 = self.task.prow
            self.pcolum0 = self.task.pcolum 
            self.task.icolum-=1
            self.task.jcolum-=1
            self.task.kcolum-=1
            self.task.pcolum-=1
            if self.task.icolum<0:
                self.task.icolum = 0
                self.task.jcolum = self.jcolum0
                self.task.kcolum = self.kcolum0
                self.task.pcolum = self.pcolum0
            self.update_block()   
            event.accept()
        elif pressedkey == Qt.Key.Key_Right:
            print('Key_Right')       
            self.irow0 = self.task.irow
            self.icolum0 = self.task.icolum
            self.jrow0 = self.task.jrow
            self.jcolum0 = self.task.jcolum
            self.krow0 = self.task.krow
            self.kcolum0 = self.task.kcolum
            self.prow0 = self.task.prow
            self.pcolum0 = self.task.pcolum
            self.task.icolum+=1
            self.task.jcolum+=1
            self.task.kcolum+=1
            self.task.pcolum+=1
            if self.task.pcolum>9:
                self.task.icolum = self.icolum0
                self.task.jcolum = self.jcolum0
                self.task.kcolum = self.kcolum0
                self.task.pcolum = 9
            self.update_block()   
            event.accept()
        elif pressedkey == Qt.Key.Key_Space:
            print('Key_Space')       
            self.timer.stop()   
            event.accept()
        else:
            event.ignore()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex=Window()
    sys.exit(app.exec())
